package neatlogic.module.change.api;


import neatlogic.framework.auth.core.AuthAction;
import neatlogic.framework.change.constvalue.*;
import neatlogic.framework.common.constvalue.ApiParamType;
import neatlogic.framework.common.constvalue.GroupSearch;
import neatlogic.framework.crossover.CrossoverServiceFactory;
import neatlogic.framework.process.auth.PROCESS_BASE;
import neatlogic.framework.process.crossover.IProcessStepHandlerCrossoverUtil;
import neatlogic.framework.process.crossover.IProcessTaskCrossoverMapper;
import neatlogic.framework.process.dto.ProcessTaskStepRemindVo;
import neatlogic.framework.process.dto.ProcessTaskStepVo;
import neatlogic.framework.restful.annotation.Description;
import neatlogic.framework.restful.annotation.Input;
import neatlogic.framework.restful.annotation.OperationType;
import neatlogic.framework.restful.annotation.Param;
import neatlogic.framework.restful.constvalue.OperationTypeEnum;
import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase;
import neatlogic.module.change.dao.mapper.ChangeMapper;
import neatlogic.framework.change.dto.ChangeStepVo;
import neatlogic.framework.change.dto.ChangeVo;
import neatlogic.framework.change.dto.ProcessTaskStepChangeVo;
import neatlogic.framework.change.exception.ChangeHandleHasNotStartedException;
import neatlogic.framework.change.exception.ChangeNoPermissionException;
import neatlogic.framework.change.exception.ChangeNotFoundException;
import neatlogic.module.change.service.ChangeService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@AuthAction(action = PROCESS_BASE.class)
@OperationType(type = OperationTypeEnum.OPERATE)
public class ChangeRecoverApi extends PrivateApiComponentBase {

    @Autowired
    private ChangeMapper changeMapper;

    @Autowired
    private ChangeService changeService;

    @Override
    public String getToken() {
        return "change/recover";
    }

    @Override
    public String getName() {
        return "nmca.changerecoverapi.getname";
    }

    @Override
    public String getConfig() {
        return null;
    }

    @Input({
            @Param(name = "changeId", type = ApiParamType.LONG, isRequired = true, desc = "term.change.changeid"),
            @Param(name = "content", type = ApiParamType.STRING, desc = "common.content"),
            @Param(name = "source", type = ApiParamType.STRING, desc = "common.source")
    })
    @Description(desc = "nmca.changerecoverapi.getname")
    @Override
    public Object myDoService(JSONObject jsonObj) throws Exception {
        Long changeId = jsonObj.getLong("changeId");
        ChangeVo changeVo = changeMapper.getChangeById(changeId);
        if (changeVo == null) {
            throw new ChangeNotFoundException(changeId);
        }
        /** 获取当前变更锁 **/
        changeMapper.getChangeLockById(changeId);

        String owner = changeMapper.getChangeUserByChangeId(changeId);
        if (StringUtils.isNotBlank(owner)) {
            changeVo.setOwner(GroupSearch.USER.getValuePlugin() + owner);
        }
        changeVo.setChangeStepList(changeMapper.getChangeStepListByChangeId(changeId));
        if (!changeService.isRecoverableChange(changeVo)) {
            throw new ChangeNoPermissionException(ChangeOperationType.RECOVERCHANGE.getText());
        }
        /** 恢复变更时，将变更状态设置为“处理中” **/
        changeVo.setStatus(ChangeStatus.RUNNING.getValue());
        changeMapper.recoverChangeStatus(changeVo);

        /** 如果是变更步骤处理人暂停变更，将对应的变更步骤状态恢复 **/
        Long changeStepId = changeMapper.getChangeStepPauseOperateChangeStepIdByChangeId(changeId);
        if (changeStepId != null) {
            ChangeStepVo changeStepVo = changeMapper.getChangeStepById(changeStepId);
            if (changeStepVo.getStartTime() != null) {
                changeStepVo.setStatus(ChangeStatus.RUNNING.getValue());
            } else {
                changeStepVo.setStatus(ChangeStatus.PENDING.getValue());
            }
            changeMapper.updateChangeStepStatus(changeStepVo);
            changeMapper.deleteChangeStepPauseOperateByChangeId(changeId);
        }

        /** 生成活动 **/
        ProcessTaskStepChangeVo processTaskStepChangeVo = changeMapper.getProcessTaskStepChangeHandleByChangeId(changeId);
        if (processTaskStepChangeVo == null) {
            throw new ChangeHandleHasNotStartedException();
        }
        IProcessTaskCrossoverMapper processTaskCrossoverMapper = CrossoverServiceFactory.getApi(IProcessTaskCrossoverMapper.class);
        ProcessTaskStepVo currentProcessTaskStepVo = processTaskCrossoverMapper.getProcessTaskStepBaseInfoById(processTaskStepChangeVo.getProcessTaskStepId());
        currentProcessTaskStepVo.getParamObj().putAll(jsonObj);
        IProcessStepHandlerCrossoverUtil processStepHandlerCrossoverUtil = CrossoverServiceFactory.getApi(IProcessStepHandlerCrossoverUtil.class);
        processStepHandlerCrossoverUtil.audit(currentProcessTaskStepVo, ChangeAuditType.RECOVERCHANGE);
        processStepHandlerCrossoverUtil.notify(currentProcessTaskStepVo, ChangeTriggerType.RECOVERCHANGE);
        processStepHandlerCrossoverUtil.action(currentProcessTaskStepVo, ChangeTriggerType.RECOVERCHANGE);
        processTaskCrossoverMapper.deleteProcessTaskStepRemind(new ProcessTaskStepRemindVo(processTaskStepChangeVo.getProcessTaskStepId(), ChangeRemindType.PAUSECHANGE.getValue()));
        return null;
    }

}
